using SimpleJSON;
using UnityEngine;
using UnityEngine.UI;

namespace Gj
{
    public class ImageHelper : NodeHelper
    {
        public Color disableColor = new(255, 255, 255, 0.4f);
        public Color originColor = new(255, 255, 255);
        public Sprite disableSprite;
        public Sprite originSprite;
        [SelectRef(UIKey.ICON_KEY)] public string type;
        public bool raw;
        public bool local;
        public bool online;
        public bool simple;
        public bool bindKey;
        public string category;

        [SelectRef(UIKey.DATA_KEY)] public SelectKey disabledKey;
        public bool invert;
        private Image _Image;
        private RawImage _RawImage;
        protected bool disabled;
        private string path;

        private Image Image
        {
            get
            {
                if (_Image == null) _Image = GetComponent<Image>();
                return _Image;
            }
        }

        private RawImage RawImage
        {
            get
            {
                if (_RawImage == null) _RawImage = GetComponent<RawImage>();
                return _RawImage;
            }
        }

        protected override BindDataType bindDataType => BindDataType.String;

        public override void SetData(JSONObject json)
        {
            if (!disabledKey.IsEmpty())
            {
                disabled = invert ? !disabledKey.Get(json).AsBool : disabledKey.Get(json).AsBool;
                if (disabled)
                    Disabled();
                else
                    Enabled();
            }

            base.SetData(json);
            if (bindKey) BindData(key);
        }

        protected override void BindData(string s)
        {
            if (StringTools.IsEmpty(s)) return;
            var text = s;
            if (local) text = Localization.Instance.GetLocalValue(s);
            if (online) text = Localization.Instance.GetOnlineValue(s);
            string _path;
            if (StringTools.IsEmpty(category))
                _path = string.Format("{0}/{1}/{2}", "Icons", type, text);
            else
                _path = string.Format("{0}/{1}/{2}/{3}", "Icons", type, CacheTools.GetVal(category, ""), text);
            if (path == _path) return;
            path = _path;
            Refresh();
            if (!gameObject.activeSelf) gameObject.SetActive(true);
        }

        protected override void BindData(float f)
        {
            Image.fillAmount = f;
        }

        protected override void BindData(bool b)
        {
            if (b)
                Enabled();
            else
                Disabled();
        }

        private void Refresh()
        {
            if (raw)
            {
                var texture = Resource.LoadAsset<Texture>(path);
                if (texture != null) RawImage.texture = texture;
            }
            else
            {
                var sprite = Resource.LoadAsset<Sprite>(path);
                if (sprite != null) Image.sprite = sprite;
            }
        }

        private void Disabled()
        {
            disabled = true;
            if (raw)
                RawImage.color = disableColor;
            else
                Image.color = disableColor;
            if (disableSprite != null) Image.sprite = disableSprite;
        }

        private void Enabled()
        {
            disabled = false;
            if (raw)
                RawImage.color = originColor;
            else
                Image.color = originColor;
            if (originSprite != null) Image.sprite = originSprite;
        }
    }
}